package com.ruoyi.erecord.iot.controller;

import com.alibaba.fastjson.TypeReference;
import com.ruoyi.erecord.iot.handle.IotDataHandle;
import com.ruoyi.erecord.iot.models.ApiPara;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import topevery.syn.data.model.EquipmentData;
import topevery.syn.data.model.EquipmentInfo;
import topevery.syn.data.model.UtilsHelper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/***
 * 接入api，需要实现 EquipmentDataHandle
 * */

@Slf4j
@RestController
@RequestMapping("/iotDataHandle")
public class IotDataHandleController {
    private static final Exception checkEx = new Exception("Check Exception");

    /***
     * 接入api，需要实现 EquipmentDataHandle
     * */
    @Resource
    private IotDataHandle dataHandle;

    public IotDataHandleController(IotDataHandle dataHandle) {
        this.dataHandle = dataHandle;
    }

    private String getUid(HttpServletRequest request) {
        return request.getHeader("uid");
    }

    private String getPwd(HttpServletRequest request) {
        return request.getHeader("pwd");
    }

    private String getLabelCode(HttpServletRequest request) {
        return request.getHeader("labelCode");
    }

    private void checkHeader(HttpServletRequest request) throws Exception {
        String uid = getUid(request);
        String pwd = getPwd(request);
        if (!dataHandle.checkHeader(uid, pwd)) {
            throw checkEx;
        }
    }

    /**
     * 返回设备列表
     **/
    @GetMapping("getEquipmentInfos")
    public String getEquipmentInfos(HttpServletRequest request) throws Exception {
        log.info("call getEquipmentInfos <<<<<");
        checkHeader(request);
        String labelCode = getLabelCode(request);
        List<EquipmentInfo> eis = dataHandle.getEquipmentInfos(labelCode);
        return UtilsHelper.stringCompressJson(eis);
    }

    @GetMapping("deviceCodes")
    public List<EquipmentInfo> deviceCodes(HttpServletRequest request) throws Exception {
        log.info("call getEquipmentInfos <<<<<");
        checkHeader(request);
        String labelCode = getLabelCode(request);
        return dataHandle.getEquipmentInfos(labelCode);
    }

    /**
     * 保存数据
     **/
    @PostMapping("setEquipmentDatas")
    public void setEquipmentDatas(HttpServletRequest request, @RequestBody ApiPara<String> para) throws Exception {
        log.info("call setEquipmentDatas >>>>>> ");
        checkHeader(request);
        String labelCode = getLabelCode(request);
        log.info("data-> {}\r\n", para.data);
        List<EquipmentData> listEquipmentData = UtilsHelper.stringDecompressJson(para.data, new TypeReference<List<EquipmentData>>() {
        });
        if (Optional.ofNullable(listEquipmentData).isPresent()) {
            dataHandle.setEquipmentDatas(labelCode, listEquipmentData);
        }
    }

    //    @PostMapping("testInsert/{count}")
    //    public void testInsert(@PathVariable("count") int count) throws Exception {
    //        log.info("testInsert  count");
    //        String filePath = "D:\\data\\work\\peng20230417_224243\\iot.process.log";
    //        List<String> stars = Files.lines(Paths.get(filePath)).collect(Collectors.toList());
    //        int k = 0;
    //        for (String str : stars) {
    //            if (k == count && count > 0) break;
    //            log.info(" row {}", k + 1);
    //            List<EquipmentData> listEquipmentData = UtilsHelper.stringDecompressJson(str, new TypeReference<List<EquipmentData>>() {
    //            });
    //            if (Optional.ofNullable(listEquipmentData).isPresent()) {
    //                dataHandle.setEquipmentDatas("topevery_check_sa", listEquipmentData);
    //            }
    //            k++;
    //
    //        }
    //    }


}
